import { Loading, MessageBox, Message, Notification } from 'element-ui'

let loadingInstance

export function msg(title, close = () => {}) {
  return Message({ message: title, showClose: true, type: 'info', onClose: close })
}

export function msgSuccess(title, close = () => {}) {
  return Message({ message: title, showClose: true, type: 'success', onClose: close })
}

export function msgError(title, close = () => {}) {
  return Message({ message: title, showClose: true, type: 'error', onClose: close })
}

export function msgWarning(title, close = () => {}) {
  return Message({ message: title, showClose: true, type: 'warning', onClose: close })
}

export function alert(content, title = '系统提示', options = {}) {
  return MessageBox.alert(content, title, {
    showCancelButton: false,
    confirmButtonText: options?.confirmButtonText || '确定',
    confirmButtonClass: options?.confirmButtonClass || 'message-box-confirm_btn',
    type: options?.type || '',
    showClose: true
  })
}

export function alertError(content, title = '系统提示', options = {}) {
  return alert(content, title, { type: 'error', ...options })
}

export function alertSuccess(content, title = '系统提示', options = {}) {
  return alert(content, title, { type: 'success', ...options })
}

export function alertWarning(content, title = '系统提示', options = {}) {
  return alert(content, title, { type: 'warning', ...options })
}

export function notify(content, title = '', options = {}) {
  return Notification({
    title: title,
    message: content,
    duration: 'duration' in options ? options.duration : 4500,
    type: options?.type || 'info',
    position: options?.position || 'top-right',
    showClose: 'showClose' in options ? options.showClose : true,
    onClose: options?.onClose,
    onClick: options?.onClick
  })
}

export function notifyError(content, title = '', options = {}) {
  return notify(content, title, { type: 'error', ...options })
}

export function notifySuccess(content, title = '', options = {}) {
  return notify(content, title, { type: 'success', ...options })
}

export function notifyWarning(content, title = '', options = {}) {
  return notify(content, title, { type: 'warning', ...options })
}

export function loading(content) {
  loadingInstance = Loading.service({
    text: content ? content : '',
    background: 'rgba(0,0,0,.4)',
    spinner: 'el-icon-loading'
  })
  return loadingInstance
}

export function closeLoading() {
  if (loadingInstance) loadingInstance.close()
}

export const confirm = (content, title = '系统提示', options = {}) => {
  options = options ? options : {}
  return MessageBox.confirm(content, title, {
    cancelButtonText: options?.cancelButtonText || '取消',
    confirmButtonText: options?.confirmButtonText || '确定',
    confirmButtonClass: options?.confirmButtonClass || 'message-box-confirm_btn',
    type: options?.type || 'warning',
    showClose: true
  })
}

export const prompt = (content, title = '系统提示', options = {}) => {
  return MessageBox.prompt(content, title, {
    cancelButtonText: options?.cancelButtonText || '取消',
    confirmButtonText: options?.confirmButtonText || '确定',
    confirmButtonClass: options?.confirmButtonClass || 'message-box-confirm_btn',
    inputPlaceholder: options?.inputPlaceholder || '', //输入框的占位符
    inputType: options?.inputType || 'text', //输入框的类型
    inputValue: options?.inputValue || '', //输入框的初始文本
    inputErrorMessage: options?.inputErrorMessage || '输入的数据不合法', //校验未通过时的提示文本
    inputValidator: options?.inputValidator || null,
    showClose: true
  })
}
